package com.test.repository;


import com.test.dto.SfConfigDTO;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by a on 2017/4/27.
 */
public class SfConfigRepositoryImpl {
    @PersistenceContext
    protected EntityManager entityManager;
    public List<SfConfigDTO> getSfConfig(){

        StringBuffer sb=new StringBuffer("select b.access_code,b.checkword,b.http_url,b.img_path,"
                + "sf.area_name,sf.custid,sf.j_address,sf.j_tel,sf.j_contact,sf.j_company "
                + "from oc_exp_base_config b,oc_exp_base_config_sf sf "
                + "where b.express_id = sf.express_id");


        Query query= entityManager.createNativeQuery(sb.toString());
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
//        pagedQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.TO_LIST);
//        List<SfConfigDTO> singlePage = query.getResultList();
        List<Map<String,?>> rows = query.getResultList();
        List<SfConfigDTO> dtoList = new ArrayList<SfConfigDTO>();
        for(Map row : rows){
            SfConfigDTO dto = new SfConfigDTO();
            dto.setHttpUrl((String)row.get("http_url"));
            dto.setAccessCode((String)row.get("access_code"));
            dto.setCheckword((String)row.get("checkword"));
            dto.setImgPath((String)row.get("img_path"));
            dto.setAreaName((String)row.get("area_name"));
            dto.setCustid((String)row.get("custid"));
            dto.setJ_company((String)row.get("j_company"));
            dto.setJ_address((String)row.get("j_address"));
            dto.setJ_contact((String)row.get("j_contact"));
            dto.setJ_tel((String)row.get("j_tel"));
            dtoList.add(dto);
        }
        return dtoList;
    }
}
